<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
    <title>电子签2</title>
    <link rel="stylesheet" href="../style/vgui-dialog.css">
    <link rel="stylesheet" href="../style/reset.css">
    <link rel="stylesheet" href="../style/index.css">
    <script src="../js/flex.min.js"></script>
    <script src="../js/zepto.js"></script>
    <script src="../js/vgui-dialog.js"></script>
    <style id="loading_style">
        .push_dialog_content .push_dialog {
            background: none;
        }
    </style>
    <style>
        .box.empty {
            height: 100%;
            width: 100%;
            position: absolute;
            top: 0px;
            pointer-events: none;
            text-align: center;
        }
        
        .box.empty img {
            width: 40%;
            margin-top: 50%;
        }
        
        .box.empty .msg {
            font-size: 14px;
            color: #ccc;
            letter-spacing: 4PX;
            margin-top: 30px;
        }
    </style>
</head>

<body>
    <div class="authPage">

        <ul id="companyList">

        </ul>
        <div class="box empty">
            <img class="img" src="../images/empty-image-default.png" />
            <div class="msg">
                暂无企业数据
            </div>
        </div>

    </div>
    <script>
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null)
                return decodeURI(r[2]);
            return null;
        }

        try {
            !!getUrlParam("tenantId") && sessionStorage.setItem("tenant_id", getUrlParam("tenantId"));
            !!getUrlParam("staffNo") && sessionStorage.setItem("staff_no", getUrlParam("staffNo"));
            !!getUrlParam("ownid") && sessionStorage.setItem("ownid", getUrlParam("ownid"));
        } catch (error) {
            console.log(error + "错误");
        }
        var loading = vgui.PushDialog.loading("加载中")
        $.ajax({
            url: "http://192.168.2.145:8000/api/electronic/getAuthOfCompanyByUserId",
            type: "GET",
            cache: false,
            data: {
                tenantId: sessionStorage.getItem("tenant_id"),
                userId: sessionStorage.getItem("ownid")
            },
            success: function(res) {
                var html = ""
                $.each(res.data, function(index, item) {

                    html += ['<li class="item" data-id=' + (item.isAuth ? (item.isWarrant ? (item.isBelong ? "" : item.id) : item.id) : item.id) + '>',
                        '<div class="companyName">' + item.companyName + '</div>',
                        '<div class="status" style=" color:' + (item.isAuth ? (item.isWarrant ? (item.isBelong ? "#2196f3" : "#ffa500") : "#ffa500") : "") + ';" >' + (item.isAuth ? (item.isWarrant ? (item.isBelong ? "已认证" : "未加入公司") : "未授权") : "待认证") + '</div>',
                        '</li>',
                    ].join("");
                })
                $("#companyList").html(html);

            },
            complete: function(xhr) {
                vgui.PushDialog.close(loading);
                $('#loading_style').remove();
                if (xhr.status !== 200) {
                    vgui.PushDialog.alert("网络错误");
                }
            }

        })
        $("#companyList").on("click", ".item", function() {
            var id = $(this).data("id");
            if (!id) {
                return
            }
            var loading = vgui.PushDialog.loading("跳转中")
            $.ajax({
                url: "/electronic/getRedirectURL",
                type: "GET",
                cache: false,
                data: {
                    userId: sessionStorage.getItem("ownid"),
                    tenantId: sessionStorage.getItem("tenant_id"),
                    companyId: id
                },
                success: function(res) {
                    if (res.result) {
                        window.location.href = res.data
                    } else {
                        vgui.PushDialog.alert(res.msg);
                    }
                },
                complete: function() {
                    vgui.PushDialog.close(loading);
                },
                error: function() {
                    vgui.PushDialog.alert("请求错误");
                }
            })
        })
    </script>
</body>

</html>